package cn.edu.sgu.www.mhxysy.service.repository.impl;

import cn.edu.sgu.www.mhxysy.entity.prop.Prop;
import cn.edu.sgu.www.mhxysy.entity.repository.RepositoryGrid;
import cn.edu.sgu.www.mhxysy.entity.repository.RepositoryProp;
import cn.edu.sgu.www.mhxysy.entity.repository.RoleRepository;
import cn.edu.sgu.www.mhxysy.mapper.prop.PropMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.RepositoryGridMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.RepositoryPropMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.RoleRepositoryMapper;
import cn.edu.sgu.www.mhxysy.service.repository.RepositoryPropService;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.vo.prop.PropVO;
import cn.edu.sgu.www.mhxysy.vo.repository.RepositoryPropVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Repository
public class RepositoryPropServiceImpl implements RepositoryPropService {

    private final PropMapper propMapper;
    private final RoleRepositoryMapper roleRepositoryMapper;
    private final RepositoryGridMapper repositoryGridMapper;
    private final RepositoryPropMapper repositoryPropMapper;

    @Autowired
    public RepositoryPropServiceImpl(
            PropMapper propMapper,
            RoleRepositoryMapper roleRepositoryMapper,
            RepositoryGridMapper repositoryGridMapper,
            RepositoryPropMapper repositoryPropMapper) {
        this.propMapper = propMapper;
        this.roleRepositoryMapper = roleRepositoryMapper;
        this.repositoryGridMapper = repositoryGridMapper;
        this.repositoryPropMapper = repositoryPropMapper;
    }

    @Override
    public void arrange(String roleId) {
        // 查询角色仓库列表
        Map<String, Object> columnMap = new HashMap<>();

        columnMap.put("role_id", roleId);

        List<RoleRepository> repositories = roleRepositoryMapper.selectByMap(columnMap);

        if (CollectionUtils.isNotEmpty(repositories)) {
            // 遍历仓库，对每个仓库的物品进行整理
            for (RoleRepository repository : repositories) {
                // 查询当前仓库的所有格子
                QueryWrapper<RepositoryGrid> queryWrapper = new QueryWrapper<>();

                queryWrapper.select("id");
                queryWrapper.orderByAsc("location");
                queryWrapper.eq("repository_id", repository.getId());

                List<RepositoryGrid> repositoryGrids = repositoryGridMapper.selectList(queryWrapper);

                // 得到格子ID列表
                List<String> gridIds = repositoryGrids.stream().map(new Function<RepositoryGrid, String>() {
                    @Override
                    public String apply(RepositoryGrid repositoryGrid) {
                        return repositoryGrid.getId();
                    }
                }).collect(Collectors.toList());

                // 查询当前仓库的所有道具
                QueryWrapper<RepositoryProp> queryWrapper1 = new QueryWrapper<>();

                queryWrapper1.in("grid_id", gridIds);

                List<RepositoryProp> repositoryProps = repositoryPropMapper.selectList(queryWrapper1);

                // 仓库没有装满
                gridIds = repositoryProps.stream().map(new Function<RepositoryProp, String>() {
                    @Override
                    public String apply(RepositoryProp repositoryProp) {
                        return repositoryProp.getGridId();
                    }
                }).collect(Collectors.toList());

                if (gridIds.size() < 25) {
                    // 整理格子，让位置自动调整
                    AtomicInteger location = new AtomicInteger();

                    for (RepositoryGrid repositoryGrid : repositoryGrids) {
                        String gridId = repositoryGrid.getId();

                        if (gridIds.contains(gridId)) {
                            location.addAndGet(1); // 当前格子位置+1

                            // 查询格子放置的道具
                            QueryWrapper<RepositoryProp> queryWrapper2 = new QueryWrapper<>();

                            queryWrapper2.eq("grid_id", gridId);

                            RepositoryProp repositoryProp = repositoryPropMapper.selectOne(queryWrapper2);

                            // 将道具放到前面位置为location的格子
                            // 得到对应位置的仓库格子
                            RepositoryGrid grid = repositoryGrids.get(location.get() - 1);

                            // 1、修改道具所在的格子ID
                            repositoryProp.setGridId(grid.getId());

                            repositoryPropMapper.updateById(repositoryProp);

                            // 2、修改道具位置
                            UpdateWrapper<Prop> updateWrapper = new UpdateWrapper<>();

                            updateWrapper.eq("id", repositoryProp.getPropId());
                            updateWrapper.set("location", location.get());

                            propMapper.update(updateWrapper.getEntity(), updateWrapper);
                        }
                    }
                }
            }
        }
    }

    @Override
    public List<PropVO> selectByRepositoryId(String repositoryId) {
        return repositoryPropMapper.selectByRepositoryId(repositoryId);
    }

    @Override
    public List<RepositoryPropVO> selectByName(String name, String roleId) {
        return repositoryPropMapper.selectByName(name, roleId);
    }

}